package main.ressik;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;


public class Main extends TabActivity {
	private AddFoodController afc;
	private AutoCompleteTextView food_text;
	private AutoCompleteTextView cal_txt;
	private AutoCompleteTextView fat_txt;
	private AutoCompleteTextView fiber_txt;
	private TabHost mTabHost;
	private Button addFood_btn;
	private static Main instance;
	private FoodListView flv;
	private CalendarView cv;
	
	
	
	
	/**
	 * Singleton
	 */
	public static Main getInstance(){
		if (instance == null){
			instance = new Main();
		}
		return instance;
	}
	
	
	
	
	
	/**
	 * Constructor.  This needs to be public because the API accesses it.  
	 */
	public Main(){
		instance = this;
		flv = new FoodListView();
		cv = new CalendarView();
		afc = new AddFoodController();
	}
	
	
	
	
	
   /**
    * This is equivalent to the main method.  This is where the application starts.
    */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    //Creates the Database and Builds the table
        FoodDb.db = openOrCreateDatabase(FoodDb.FOOD_DB_NAME, Main.MODE_WORLD_READABLE, null);
        FoodDb.db.execSQL("CREATE TABLE IF NOT EXISTS " + FoodDb.FOOD_TABLE_NAME + "(name VARCHAR, cals VARCHAR, fat VARCHAR, fiber VARCHAR)");
        FoodDb.db.execSQL("CREATE TABLE IF NOT EXISTS " + FoodDb.DATE_TABLE_NAME + "(name VARCHAR, date VARCHAR)");
    //Assigns the components to a variable
        food_text = (AutoCompleteTextView) findViewById(R.id.foodTxt);
        cal_txt = (AutoCompleteTextView) findViewById(R.id.calTxt);
        fat_txt = (AutoCompleteTextView) findViewById(R.id.fatTxt);
        fiber_txt = (AutoCompleteTextView) findViewById(R.id.fiberTxt);
    //Set up the Tabs
        mTabHost = super.getTabHost();
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator(getString(R.string.food_manager)).setContent(R.id.addFoodTab));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator(getString(R.string.added_foods)).setContent(R.id.addedFoodsTab));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator(getString(R.string.cal)).setContent(R.id.date_tab));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator(getString(R.string.about)).setContent(R.id.aboutTab));
    //Creates the "About Tab"
        TextView about_tab = (TextView) findViewById(R.id.aboutTab);
        about_tab.setText(R.string.about_txt);	
    //Builds List Views
		flv.buildList();
		cv.buildCalFoodList();
    //onClick of the Add Food Button
        addFood_btn = (Button) findViewById(R.id.addFoodBtn);   
        addFood_btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (addFood_btn.getText().equals("Add Food")){
					afc.addFoodClick(food_text,cal_txt, fat_txt, fiber_txt, Main.this, addFood_btn);
					flv.buildList();
				}
				else if (addFood_btn.getText().equals("Update Food")){
					afc.updateFoodClick(food_text,cal_txt, fat_txt, fiber_txt, Main.this, addFood_btn);
				}
				else {
					new AlertDialog.Builder(Main.this).setTitle("Error:").setMessage("There was a problem.  Please email this problem to the devleoper").show();
				}
        	}
        });
    }
	
	
	
	
	
	//Getters and Setters
	public AutoCompleteTextView getFood_text() {
		return food_text;
	}
	
	
	public AutoCompleteTextView getCal_txt() {
		return cal_txt;
	}
	
	
	public AutoCompleteTextView getFat_txt() {
		return fat_txt;
	}
	
	
	public AutoCompleteTextView getFiber_txt() {
		return fiber_txt;
	}
	
	
	public FoodListView getFlv() {
		return flv;
	}
	
	
	public Button getAddFood_btn() {
		return addFood_btn;
	}
	
	
	
}
